﻿@model CategoryModel

@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)

<script>
    $(document).ready(function() {
        bindBootstrapTabSelectEvent('category-edit');
    });
</script>

<div class="content">
    <div class="form-horizontal">
        <div id="category-edit" class="nav-tabs-custom">
            <ul class="nav nav-tabs">
                @Html.RenderBootstrapTabHeader("tab-info", @T("Admin.Catalog.Categories.Info"), true)
                @Html.RenderBootstrapTabHeader("tab-seo", @T("Admin.Common.SEO"))
                @Html.RenderBootstrapTabHeader("tab-products", @T("Admin.Catalog.Categories.Products"))
            </ul>
            <div class="tab-content">
                @Html.RenderBootstrapTabContent("tab-info", @TabInfo(), true)
                @Html.RenderBootstrapTabContent("tab-seo", @TabSeo())
                @Html.RenderBootstrapTabContent("tab-products", @TabProducts())
            </div>
        </div>
    </div>
</div>

@{
    //custom tabs
    var eventMessage = new AdminTabStripCreated(this.Html, "category-edit");
    EngineContext.Current.Resolve<IEventPublisher>().Publish(eventMessage);
    foreach (var eventBlock in eventMessage.BlocksToRender)
    {
        @eventBlock
    }
}

@*save selected tab name*@
<input type="hidden" id="selected-tab-name" name="selected-tab-name" value="@(Html.GetSelectedTabName())">

@helper TabInfo()
{
    <script type="text/javascript">
        $(document).ready(function () {
            $("#@Html.FieldIdFor(model => model.AllowCustomersToSelectPageSize)").click(togglePageSize);
            togglePageSize();
        });

        function togglePageSize() {
            if ($('#@Html.FieldIdFor(model => model.AllowCustomersToSelectPageSize)').is(':checked')) {
                $('#pnlPageSize').hide();
                $('#pnlPageSizeOptions').show();
            } else {
                $('#pnlPageSize').show();
                $('#pnlPageSizeOptions').hide();
            }
        }
    </script>

    <div class="panel-group">
        <div class="panel panel-default">
            <div class="panel-body">
                @(Html.LocalizedEditor<CategoryModel, CategoryLocalizedModel>("category-info-localized",
                @<div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.SDFLabelFor(model => model.Locales[item].Name)
                        </div>
                        <div class="col-md-9">
                            @Html.SDFEditorFor(model => model.Locales[item].Name)
                            @Html.ValidationMessageFor(model => model.Locales[item].Name)
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.SDFLabelFor(model => model.Locales[item].Description)
                        </div>
                        <div class="col-md-9">
                            @Html.EditorFor(model => model.Locales[item].Description, "RichEditor")
                            @Html.ValidationMessageFor(model => model.Locales[item].Description)
                        </div>
                    </div>
                    @Html.HiddenFor(model => model.Locales[item].LanguageId)
                </div>
                ,
                @<div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.SDFLabelFor(model => model.Name)
                        </div>
                        <div class="col-md-9">
                            @Html.SDFEditorFor(model => model.Name)
                            @Html.ValidationMessageFor(model => model.Name)
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.SDFLabelFor(model => model.Description)
                        </div>
                        <div class="col-md-9">
                            @Html.EditorFor(x => x.Description, "RichEditor")
                            @Html.ValidationMessageFor(model => model.Description)
                        </div>
                    </div>
                </div>
                  ))

                <div class="form-group" @(Model.AvailableCategoryTemplates.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.CategoryTemplateId)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFDropDownListFor(model => model.CategoryTemplateId, Model.AvailableCategoryTemplates)
                        @Html.ValidationMessageFor(model => model.CategoryTemplateId)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.PictureId)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(model => model.PictureId)
                        @Html.ValidationMessageFor(model => model.PictureId)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.ParentCategoryId)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFDropDownListFor(model => model.ParentCategoryId, Model.AvailableCategories)
                        @Html.ValidationMessageFor(model => model.ParentCategoryId)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.PriceRanges)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(x => x.PriceRanges)
                        @Html.ValidationMessageFor(model => model.PriceRanges)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.ShowOnHomePage)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(x => x.ShowOnHomePage)
                        @Html.ValidationMessageFor(model => model.ShowOnHomePage)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.IncludeInTopMenu)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(x => x.IncludeInTopMenu)
                        @Html.ValidationMessageFor(model => model.IncludeInTopMenu)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.AllowCustomersToSelectPageSize)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(x => x.AllowCustomersToSelectPageSize)
                        @Html.ValidationMessageFor(model => model.AllowCustomersToSelectPageSize)
                    </div>
                </div>
                <div id="pnlPageSize" class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.PageSize)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(x => x.PageSize)
                        @Html.ValidationMessageFor(model => model.PageSize)
                    </div>
                </div>
                <div id="pnlPageSizeOptions" class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.PageSizeOptions)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(x => x.PageSizeOptions)
                        @Html.ValidationMessageFor(model => model.PageSizeOptions)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.SelectedDiscountIds)
                    </div>
                    <div class="col-md-9">
                        @Html.EditorFor(model => model.SelectedDiscountIds, new {SelectList = Model.AvailableDiscounts})
                        <script>
                            $(document).ready(function() {
                                var discountsIdsInput = $('#@Html.FieldIdFor(model => model.SelectedDiscountIds)').data("kendoMultiSelect");
                                discountsIdsInput.setOptions({
                                    autoClose: false
                                });

                                @if (Model.AvailableDiscounts.Count == 0)
                                {
                                    <text>
                                discountsIdsInput.setOptions({
                                    enable: false,
                                    placeholder: '@T("Admin.Catalog.Categories.Fields.Discounts.NoDiscounts")'
                                });
                                discountsIdsInput._placeholder();
                                discountsIdsInput._enable();
                                </text>
                                }
                            });
                        </script>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.SelectedCustomerRoleIds)
                    </div>
                    <div class="col-md-9">
                        <div class="row">
                            <div class="col-md-4">
                                @Html.EditorFor(model => model.SelectedCustomerRoleIds, new {SelectList = Model.AvailableCustomerRoles})
                                <script>
                                    $(document).ready(function() {
                                        var rolesIdsInput = $('#@Html.FieldIdFor(model => model.SelectedCustomerRoleIds)').data("kendoMultiSelect");
                                        rolesIdsInput.setOptions({
                                            autoClose: false
                                        });

                                        @if (Model.AvailableCustomerRoles.Count == 0)
                                        {
                                            <text>
                                        rolesIdsInput.setOptions({
                                            enable: false,
                                            placeholder: 'No customer roles available'
                                        });
                                        rolesIdsInput._placeholder();
                                        rolesIdsInput._enable();
                                    </text>
                                        }
                                    });
                                </script>
                            </div>
                            <div class="col-md-8">
                                @Html.Action("AclDisabledWarning", "Common")
                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.SelectedProjectIds)
                    </div>
                    <div class="col-md-9">
                        <div class="row">
                            <div class="col-md-4">
                                @Html.EditorFor(model => model.SelectedProjectIds, new { SelectList = Model.AvailableProjects })
                                <script>
                                $(document).ready(function() {
                                    var projectsIdsInput = $('#@Html.FieldIdFor(model => model.SelectedProjectIds)').data("kendoMultiSelect");
                                    projectsIdsInput.setOptions({
                                        autoClose: false
                                    });

                                    @if (Model.AvailableProjects.Count == 0)
                                    {
                                        <text>
                                    projectsIdsInput.setOptions({
                                        enable: false,
                                        placeholder: 'No Projects available'
                                    });
                                    projectsIdsInput._placeholder();
                                    projectsIdsInput._enable();
                                    </text>
                                    }
                                });
                                </script>
                            </div>
                            <div class="col-md-8">
                                @Html.Action("MultiprojectDisabledWarning", "Common")
                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.Published)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(x => x.Published)
                        @Html.ValidationMessageFor(model => model.Published)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.DisplayOrder)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(model => model.DisplayOrder)
                        @Html.ValidationMessageFor(model => model.DisplayOrder)
                    </div>
                </div>
            </div>
        </div>
    </div>
}

@helper TabSeo()
{
    <div class="panel-group">
        <div class="panel panel-default">
            <div class="panel-body">
                @(Html.LocalizedEditor<CategoryModel, CategoryLocalizedModel>("category-seo-localized",
                @<div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.SDFLabelFor(model => model.Locales[item].MetaKeywords)
                        </div>
                        <div class="col-md-9">
                            @Html.SDFEditorFor(model => model.Locales[item].MetaKeywords)
                            @Html.ValidationMessageFor(model => model.Locales[item].MetaKeywords)
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.SDFLabelFor(model => model.Locales[item].MetaDescription)
                        </div>
                        <div class="col-md-9">
                            @Html.SDFTextAreaFor(model => model.Locales[item].MetaDescription)
                            @Html.ValidationMessageFor(model => model.Locales[item].MetaDescription)
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.SDFLabelFor(model => model.Locales[item].MetaTitle)
                        </div>
                        <div class="col-md-9">
                            @Html.SDFEditorFor(model => model.Locales[item].MetaTitle)
                            @Html.ValidationMessageFor(model => model.Locales[item].MetaTitle)
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.SDFLabelFor(model => model.Locales[item].SeName)
                        </div>
                        <div class="col-md-9">
                            @Html.SDFEditorFor(model => model.Locales[item].SeName)
                            @Html.ValidationMessageFor(model => model.Locales[item].SeName)
                        </div>
                    </div>
                    @Html.HiddenFor(model => model.Locales[item].LanguageId)
                </div>
    ,
                @<div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.SDFLabelFor(model => model.MetaKeywords)
                        </div>
                        <div class="col-md-9">
                            @Html.SDFEditorFor(x => x.MetaKeywords)
                            @Html.ValidationMessageFor(model => model.MetaKeywords)
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.SDFLabelFor(model => model.MetaDescription)
                        </div>
                        <div class="col-md-9">
                            @Html.SDFTextAreaFor(x => x.MetaDescription)
                            @Html.ValidationMessageFor(model => model.MetaDescription)
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.SDFLabelFor(model => model.MetaTitle)
                        </div>
                        <div class="col-md-9">
                            @Html.SDFEditorFor(x => x.MetaTitle)
                            @Html.ValidationMessageFor(model => model.MetaTitle)
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.SDFLabelFor(model => model.SeName)
                        </div>
                        <div class="col-md-9">
                            @Html.SDFEditorFor(x => x.SeName)
                            @Html.ValidationMessageFor(model => model.SeName)
                        </div>
                    </div>
                </div>))
            </div>
        </div>
    </div>
}

@helper TabProducts()
{
    <div class="panel-group">
        @if (Model.Id > 0)
        {
            <div class="panel panel-default">
                <div class="panel-body">
                    @{
                        var defaultGridPageSize = EngineContext.Current.Resolve<SDF.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
                        var gridPageSizes = EngineContext.Current.Resolve<SDF.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;
                    }

                    <div id="products-grid"></div>

                    <script>
                        $(document).ready(function() {
                            $("#products-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("ProductList", "Category", new {categoryId = Model.Id}))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        },
                                        update: {
                                            url: "@Html.Raw(Url.Action("ProductUpdate", "Category"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        },
                                        destroy: {
                                            url: "@Html.Raw(Url.Action("ProductDelete", "Category"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors",
                                        model: {
                                            id: "Id",
                                            fields: {
                                                ProductName: { editable: false, type: "string" },
                                                IsFeaturedProduct: { editable: true, type: "boolean" },
                                                DisplayOrder: { editable: true, type: "number" },
                                                ProductId: { editable: false, type: "number" }
                                            }
                                        }
                                    },
                                    requestEnd: function(e) {
                                        if (e.type == "update") {
                                            this.read();
                                        }
                                    },
                                    error: function(e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    pageSize: @(defaultGridPageSize),
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    pageSizes: [@(gridPageSizes)]
                                },
                                editable: {
                                    confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                    mode: "inline"
                                },
                                scrollable: false,
                                columns: [
                                    {
                                        field: "ProductName",
                                        title: "@T("Admin.Catalog.Categories.Products.Fields.Product")"
                                    }, {
                                        field: "IsFeaturedProduct",
                                        title: "@T("Admin.Catalog.Categories.Products.Fields.IsFeaturedProduct")",
                                        width: 150,
                                        headerAttributes: { style: "text-align:center" },
                                        attributes: { style: "text-align:center" },
                                        template: '# if(IsFeaturedProduct) {# <i class="fa fa-check true-icon"></i> #} else {# <i class="fa fa-close false-icon"></i> #} #'
                                    }, {
                                        field: "DisplayOrder",
                                        title: "@T("Admin.Catalog.Categories.Products.Fields.DisplayOrder")",
                                        //integer format
                                        format: "{0:0}",
                                        width: 150
                                    }, {
                                        field: "ProductId",
                                        title: "@T("Admin.Common.View")",
                                        width: 100,
                                        template: '<a href="@Url.Content("~/Admin/Product/Edit/")#=ProductId#">@T("Admin.Common.View")</a>'
                                    }, {
                                        command: [
                                            {
                                                name: "edit",
                                                text: {
                                                    edit: "@T("Admin.Common.Edit")",
                                                    update: "@T("Admin.Common.Update")",
                                                    cancel: "@T("Admin.Common.Cancel")"
                                                }
                                            }, {
                                                name: "destroy",
                                                text: "@T("Admin.Common.Delete")"
                                            }
                                        ],
                                        width: 200
                                    }
                                ]
                            });
                        });
                    </script>
                </div>

                <div class="panel-footer">
                    <button type="submit" id="btnAddNewProduct"
                            onclick="javascript:OpenWindow('@(Url.Action("ProductAddPopup", "Category", new {categoryId = Model.Id, btnId = "btnRefreshProducts", formId = "category-form"}))', 800, 800, true); return false;"
                            class="btn btn-primary">
                        @T("Admin.Catalog.Categories.Products.AddNew")
                    </button>
                    <input type="submit" id="btnRefreshProducts" style="display: none" />
                    <script type="text/javascript">
                        $(document).ready(function () {
                            $('#btnRefreshProducts').click(function () {
                                //refresh grid
                                var grid = $("#products-grid").data('kendoGrid');
                                grid.dataSource.read();

                                //return false to don't reload a page
                                return false;
                            });
                        });
                    </script>
                </div>
            </div>
                        }
                        else
                        {
                            <div class="panel panel-default">
                                <div class="panel-body">
                                    @T("Admin.Catalog.Categories.Products.SaveBeforeEdit")
                                </div>
                            </div>
                        }
    </div>
}